package com.android.gilastv.ui.activity;

import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.android.baselibrary.interface_.OkHttpCallBack;
import com.android.baselibrary.responce.BaseResponce;
import com.android.baselibrary.tool.CommLoading;
import com.android.baselibrary.tool.CommToast;
import com.android.baselibrary.view.Comm_EditView;
import com.android.gilastv.R;
import com.android.gilastv.bean.TokenInfo;
import com.android.gilastv.manager.API_Manager;
import com.android.gilastv.manager.UserManager;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity1 extends BaseActivity {

    @BindView(R.id.et_account)
    Comm_EditView et_account;
    @BindView(R.id.et_pwd)
    Comm_EditView et_pwd;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login1;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.csb_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.csb_submit:
                loginByPwd();
                break;
        }
    }

    //登录
    private void loginByPwd() {
        final String account = et_account.getText();
        final String pwd = et_pwd.getText();

        if (TextUtils.isEmpty(account)) {
            CommToast.showToast(mContext, "请输入账号");
            et_account.requestFocus();
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            CommToast.showToast(mContext, "请输入密码");
            et_pwd.requestFocus();
            return;
        }

        CommLoading.showLoading((AppCompatActivity) mContext);
        API_Manager.loginByPwd(mContext, account, pwd, new OkHttpCallBack<BaseResponce<TokenInfo>>() {
            @Override
            public void onSuccess(BaseResponce<TokenInfo> baseResponce) {
                TokenInfo tokenInfo = baseResponce.getData();
                UserManager.saveToken(mContext, tokenInfo.tokenValue);
                CommToast.showToast(mContext, "登录成功");

                finish();
            }

            @Override
            public void onFailure(BaseResponce<TokenInfo> baseResponce) {
                CommLoading.dismissLoading();
                CommToast.showToast(mContext, baseResponce.msg);
            }
        });
    }
}
